package com.company.sparsearr;

import java.io.ByteArrayInputStream;

public class sparseArr {
    public static void main(String[] args) {
        int chessArr1[][] = new int[11][11];
        int count=0;
        chessArr1[1][2] = 1;
        chessArr1[2][3] = 2;
        chessArr1[3][4] = 1;
        chessArr1[4][5] = 2;
        System.out.println("初始数组：");
        for (int[] row : chessArr1) {
            for (int c : row
            ) {
                if (c!=0){
                    count++;
                }
                System.out.print(c + "\t");
            }
            System.out.println();
        }
        int sparseArr[][] =new int [count+1][3];
        sparseArr[0][0]=chessArr1.length;
        sparseArr[0][1]=chessArr1[0].length;
        sparseArr[0][2]=count;
        count=0;
        for (int i=0;i<chessArr1.length-1;i++){
            for (int j = 0; j < chessArr1[0].length-1 ; j++) {
                if (chessArr1[i][j]!=0){
                    count++;
                    sparseArr[count][0]=i;
                    sparseArr[count][1]=j;
                    sparseArr[count][2]=chessArr1[i][j];
                }
            }
        }
        System.out.println("稀疏数组：");
        for (int[] row : sparseArr) {
            for (int c : row
            ) {
                System.out.print(c + "\t");
            }
            System.out.println();
        }
        int chessArr2[][]=new int[sparseArr[0][0]][sparseArr[0][1]];
        for(int i=1;i<=sparseArr[0][2];i++){
            chessArr2[sparseArr[i][0]][sparseArr[i][1]]=sparseArr[i][2];
        }
        System.out.println("稀疏数组转化后：");
        for (int[] row : chessArr2) {
            for (int c : row
            ) {
                System.out.print(c + "\t");
            }
            System.out.println();
        }
    }

}
